package org.i1510.common.controller;

	import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.i1510.common.bean.PageBean;
import org.i1510.common.dao.ISysPopDao;
import org.i1510.common.po.SysPop;
import org.i1510.common.service.ISysPopService;
import org.i1510.common.utils.file.AuthBeanUtils;
import org.i1510.common.utils.web.ResultMapper;
import org.i1510.common.utils.web.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
	
	
	
	
	
	
	@Controller
	@RequestMapping(value = "/admin/sysPop")
	public class SysPopController {
		private static final Logger logger = LoggerFactory.getLogger(SysPopController.class);
		@Autowired
		private ISysPopService iSysPopService;
		@Autowired
		private ISysPopDao iSysPopDao;
		
		private ResultMapper mapper;
		
		/**
		*@Author Rambo
		*@Desc：保存对象
		*@param sysPop
		*@param errors
		*@return TODO
		*@Version  SysPopController.java,v 1.1 2013-11-15 下午4:20:56 
		*/
		@RequestMapping(value = "/save", method = RequestMethod.POST)
		@ResponseBody
		public String save(HttpServletRequest request,@Valid @ModelAttribute("sysPop") SysPop sysPop,BindingResult errors) {
			
			if(errors.hasErrors()){			
				mapper = WebUtils.getMapper4Errors(errors);
			}else{
				mapper = iSysPopService.save(sysPop,request);
			}
			
			return mapper.toJson();
		}
		/**
		*@Author Rambo
		*@Desc：删除对象
		*@param docId
		*@param errors
		*@return TODO
		*@Version  SysPopController.java,v 1.1 2014-9-2 下午3:38:17 
		*/
		@RequestMapping(value = "/deleteX", method = RequestMethod.POST)
		@ResponseBody
		public String deleteX(@Valid @ModelAttribute("docId") String docId,BindingResult errors) {
			
			if(errors.hasErrors()){			
				mapper = WebUtils.getMapper4Errors(errors);
			}else{
				mapper = iSysPopService.delete(docId);
			}
			
			return mapper.toJson();
		}
		/**
		*@Author Rambo
		*@Desc：查看对象
		*@param docId
		*@param errors
		*@return TODO
		*@Version  SysPopController.java,v 1.1 2014-9-2 下午3:39:35 
		*/
		@RequestMapping(value = "/view", method = RequestMethod.GET)
		@ResponseBody
		public String view(@Valid @ModelAttribute("docId") String docId,BindingResult errors) {
			ResultMapper mapperRtn = ResultMapper.generateFailure();
			if(errors.hasErrors()){			
				mapperRtn = WebUtils.getMapper4Errors(errors);
			}else{
				SysPop sysPop = iSysPopService.findById(docId);
				mapperRtn = ResultMapper.generateSuccess();
				mapperRtn.appendResult("data", sysPop);
			}		
			return mapperRtn.toJson();
		}
		@RequestMapping(value = "/list", method = RequestMethod.GET)
		@ResponseBody
		public String list(@ModelAttribute("fromDate") String fromDate,@ModelAttribute("toDate") String toDate,@Valid @ModelAttribute("sysPop") SysPop sysPop,@Valid @ModelAttribute("pageBean") PageBean pageBean,BindingResult errors) {
			ResultMapper mapperRtn = ResultMapper.generateFailure();
			if(errors.hasErrors()){			
				mapperRtn = WebUtils.getMapper4Errors(errors);
			}else{
				Map conditions = new HashMap();
				conditions.put("fromDate", fromDate);
				conditions.put("toDate", toDate);
				List  sysPops = iSysPopService.getPage(sysPop, pageBean,conditions);
				int totalCount =  iSysPopService.getPageCount(sysPop,conditions);
				Map map = new HashMap();
				map.put("pageNo", pageBean.getPageNo());
				map.put("pageSize", pageBean.getPageSize());
				map.put("totalCount", totalCount);
				map.put("listData",sysPops);
				
				mapperRtn = ResultMapper.generateSuccess();
				mapperRtn.appendResult("data", map);
			}		
			return mapperRtn.toJson();
		}
		@RequestMapping(value = "/reloadAuthAccess", method = RequestMethod.POST)
		@ResponseBody
		public String reloadAuthAccess(HttpServletRequest request,@ModelAttribute("loaction") String loaction,BindingResult errors) {
			
			if(errors.hasErrors()){			
				mapper = WebUtils.getMapper4Errors(errors);
				mapper.appendResult("message", "操作失败!");
			}else{
				List<SysPop> list = new ArrayList<SysPop>();
				list = AuthBeanUtils.readAuth4Request(loaction,request);
				mapper = iSysPopService.saveAll(list);
				mapper.appendResult("message", "操作成功!");
			}
			
			return mapper.toJson();
		}
		
}


